/*
https://leetcode-cn.com/problems/water-and-jug-problem/solution/shui-hu-wen-ti-by-leetcode-solution/
 */
public class Solution365 {
    public boolean canMeasureWater(int jug1Capacity, int jug2Capacity, int targetCapacity) {
        if (targetCapacity>jug1Capacity+jug2Capacity){
            return false;
        }
        if (jug1Capacity==0 || jug2Capacity==0){
            return targetCapacity==0 || jug1Capacity+jug2Capacity==targetCapacity;
        }
        return targetCapacity%gcd(jug1Capacity,jug2Capacity)==0;
    }

    private int gcd(int a, int b) {
        if (b>a){
            int t=a;
            a=b;
            b=t;
        }
        if (b==0){
            return a;
        }
        return gcd(b,a%b);
    }

    public static void main(String[] args) {
        System.out.println(new Solution365().canMeasureWater(3,5,4));
    }
}
